
#include <BChangeEvent>
#include <BStyle>

#include <BFileDialog>
#include "member_BFileDialog.h"

using namespace BWE;

#define member					(*(member_BFileDialog*)_ptr)
#define member_allocate()		_ptr = new member_BFileDialog(this)
#define member_release()		delete (member_BFileDialog*)_ptr

BFileDialog::BFileDialog(BWidget* parent) : BDialog(parent)
{
	member_allocate();
	this->setSize(800, 560);
}
BFileDialog::~BFileDialog()
{
	member_release();
}

void BFileDialog::setFilter(BFolder::Filter filter)
{
	if (member.filter != filter)
	{
		member.filter = filter;
		member.openFolder(member.pathComboBox->currentText());
	}
}
BFolder::Filter BFileDialog::filter() const
{
	return member.filter;
}

void BFileDialog::setNameFilter(const BString& nameFilter)
{
	if (member.nameFilter != nameFilter)
	{
		member.nameFilter = nameFilter;
		member.openFolder(member.pathComboBox->currentText());
	}
}
const BString& BFileDialog::nameFilter() const
{
	return member.nameFilter;
}

void BFileDialog::setSelectMode(SelectMode selectMode)
{
	if (member.selectMode != selectMode)
	{
		member.selectMode = selectMode;
		member.tableWidget->setSelectMode(selectMode);
	}
}
SelectMode BFileDialog::selectMode() const
{
	return member.selectMode;
}

void BFileDialog::setPath(const BString& path)
{
	member.locate(path);
}
const BString& BFileDialog::path() const
{
	return member.pathComboBox->currentText();
}

const BString& BFileDialog::fileName() const
{
	return member.fileNames.first();
}
const BStringList& BFileDialog::fileNames() const
{
	return member.fileNames;
}

void BFileDialog::changeEvent(const BChangeEvent& event)
{
	BDialog::changeEvent(event);
	if (event.change() == Change_Style)
	{
		member.style = event.style();
		member.dirtyStyle = true;
	}
}
void BFileDialog::freshEvent(const BEvent& event)
{
	BDialog::freshEvent(event);
	if (member.dirtyStyle)
	{
		for (int i = 0; i < member.treeWidget->topItemCount(); i++)
		{
			member.freshTreeItemImage(member.treeWidget->topItem(i));
		}
		member.freshTableItemImage();
		member.dirtyStyle = false;
	}
}
void BFileDialog::showEvent(const BEvent& event)
{
	BDialog::showEvent(event);
	if (member.pathComboBox->currentText().empty())
	{
		BString current = BFolder::Current();
		member.locate(current);
	}
}
